package com.zzx.bms.service.impl;

import com.zzx.bms.domain.Department;
import com.zzx.bms.domain.DepartmentExample;
import com.zzx.bms.domain.DepartmentType;
import com.zzx.bms.domain.DepartmentTypeExample;
import com.zzx.bms.persistence.DepartmentMapper;
import com.zzx.bms.persistence.DepartmentTypeMapper;
import com.zzx.bms.service.IDepartmentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by SamZhao on 2016/12/27.
 */
@Service(value="departmentService")
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private DepartmentTypeMapper departmentTypeMapper;

    @Override
    public List<Department> findAllDepartmentList() {
        DepartmentExample exp = new DepartmentExample();
        DepartmentExample.Criteria crt = exp.createCriteria();
        crt.andIsDeleteEqualTo("0");
        List<Department> list = departmentMapper.selectByExample(exp);
        return list;
    }

    @Override
    public int saveDepartment(Department department) {
        int flag = 0;
        if(department.getId()!=null){
            flag = departmentMapper.updateByPrimaryKeySelective(department);
        }
        else{
            flag = departmentMapper.insertSelective(department);
        }
        return flag;
    }

    @Override
    public int deleteDepartmentById(String id) {
        int flag = 0;
        DepartmentExample exp = new DepartmentExample();
        DepartmentExample.Criteria crt = exp.createCriteria();
        Department department = departmentMapper.selectByPrimaryKey(Integer.parseInt(id));
        if(department!=null){
            department.setIsDelete("1");
            flag = departmentMapper.updateByPrimaryKeySelective(department);
        }
        return flag;
    }

    @Override
    public int checkDepartment(Department department) {
        DepartmentExample exp = new DepartmentExample();
        DepartmentExample.Criteria crt = exp.createCriteria();
        if(department.getNumber()!=null)
            crt.andNumberEqualTo(department.getNumber());
        if(StringUtils.isNotEmpty(department.getName()))
            crt.andNameEqualTo(department.getName().trim());
        if(department.getId()!=null)
            crt.andIdNotEqualTo(department.getId());
        crt.andIsDeleteEqualTo("0");
        int flag = departmentMapper.countByExample(exp);
        return flag;
    }

    @Override
    public Department findDepartmentById(String id) {
        Department department = null;
        if(StringUtils.isNotEmpty(id))
            department = departmentMapper.selectByPrimaryKey(Integer.parseInt(id));
        return department;
    }

    @Override
    public List<DepartmentType> findAllDepartmentTypeList() {
        DepartmentTypeExample exp = new DepartmentTypeExample();
        DepartmentTypeExample.Criteria crt = exp.createCriteria();
        crt.andIsDeleteEqualTo("0");
        List<DepartmentType> list = departmentTypeMapper.selectByExample(exp);
        return list;
    }

    @Override
    public int saveDepartmentType(DepartmentType departmentType) {
        int flag = 0;
        if(departmentType.getId()!=null){
            flag = departmentTypeMapper.updateByPrimaryKeySelective(departmentType);
        }
        else{
            flag = departmentTypeMapper.insertSelective(departmentType);
        }
        return flag;
    }

    @Override
    public int deleteDepartmentTypeById(String id) {
        int flag = 0;
        DepartmentTypeExample exp = new DepartmentTypeExample();
        DepartmentTypeExample.Criteria crt = exp.createCriteria();
        DepartmentType departmentType = departmentTypeMapper.selectByPrimaryKey(Integer.parseInt(id));
        if(departmentType!=null){
            departmentType.setIsDelete("1");
            flag = departmentTypeMapper.updateByPrimaryKeySelective(departmentType);
        }
        return flag;
    }

    @Override
    public int checkDepartmentType(DepartmentType departmentType) {
        DepartmentTypeExample exp = new DepartmentTypeExample();
        DepartmentTypeExample.Criteria crt = exp.createCriteria();
        if(StringUtils.isNotEmpty(departmentType.getName()))
            crt.andNameEqualTo(departmentType.getName().trim());
        if(departmentType.getId()!=null)
            crt.andIdNotEqualTo(departmentType.getId());
        crt.andIsDeleteEqualTo("0");

        int flag = departmentTypeMapper.countByExample(exp);
        return flag;
    }

    @Override
    public DepartmentType findDepartmentTypeById(String id) {
        DepartmentType departmentType = null;
        if(StringUtils.isNotEmpty(id))
            departmentType = departmentTypeMapper.selectByPrimaryKey(Integer.parseInt(id));
        return departmentType;
    }
}
